package com.hwbsite.hwbback.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hwbsite.hwbback.consstants.CacheConstants;
import com.hwbsite.hwbback.consstants.ResConstant;
import com.hwbsite.hwbback.entity.InviteCode;
import com.hwbsite.hwbback.entity.UserDTO;
import com.hwbsite.hwbback.entity.UserInfo;
import com.hwbsite.hwbback.mapper.InviteCodeMapper;
import com.hwbsite.hwbback.mapper.UserInfoMapper;
import com.hwbsite.hwbback.utils.DhRes;
import com.hwbsite.hwbback.utils.FileUploader;
import com.hwbsite.hwbback.utils.UserTokenUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService{


    private final UserInfoMapper userMapper;
    private final FileUploader fileUploader;
    private final RedisTemplate redisTemplate15;
    private final CommonService commonService;

    @Override
    public UserInfo getUser(String name){
        UserInfo userInfo = userMapper.selectOne(Wrappers.<UserInfo>lambdaQuery().eq(UserInfo::getUsername, name).eq(UserInfo::getIsValid,true));
        return userInfo;
    }

    @Override
    public UserInfo getUserById(String userId){
        UserInfo userInfo = userMapper.selectOne(Wrappers.<UserInfo>lambdaQuery().eq(UserInfo::getUserId, userId).eq(UserInfo::getIsValid,true));
        return userInfo;
    }

    @Override
    public DhRes userEdit(UserDTO userDTO) {
        UserInfo userInfo =  getUserById(UserTokenUtils.getUser().getUserId()) ;
        userInfo.setNickName(userDTO.getNickName());

        if(userDTO.getAvatar().startsWith("data:image/png;base64")) {
            userInfo.setAvatar(fileUploader.uploadAvatar(userDTO.getAvatar()));
        }
        userMapper.updateById(userInfo);

        redisTemplate15.delete(CacheConstants.USER_DETAILS + userInfo.getUsername());
        return DhRes.success();
    }

    @Override
    public DhRes forget(UserDTO userDTO) {

        UserInfo userInfo =  this.getUser(userDTO.getUsername());
        if(userInfo == null ){
            return DhRes.fail(userDTO.getUsername(), ResConstant.NO_ACCOUNT);
        }

        userInfo.setPassword(new BCryptPasswordEncoder().encode(userDTO.getPassword()));
        userMapper.updateById(userInfo);
        commonService.logOut(userInfo);
        return DhRes.success();
    }

    @Override
    public DhRes userInfo(String userId) {
        return DhRes.success(userMapper.selectById(userId));
    }

    @Override
    public DhRes register(UserDTO userDTO) {
        UserInfo userInfo =  this.getUser(userDTO.getUsername());
        if(userInfo != null ){
            return DhRes.fail(userDTO.getUsername(), ResConstant.DUPLICATE_ACCOUNT);
        }
        userInfo = new UserInfo();
        userInfo.setUserId(UUID.randomUUID().toString());
        userInfo.setUsername(userDTO.getUsername());
        userInfo.setPassword(new BCryptPasswordEncoder().encode(userDTO.getPassword()));
        userInfo.setRolesId("1");
        userInfo.setNickName(userDTO.getNickName());
        userMapper.insert(userInfo);
        return DhRes.success();
    }

}
